package com.ruoyi.hxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hxf.domain.bo.PalmaryBo;
import com.ruoyi.hxf.domain.vo.PalmaryVo;
import com.ruoyi.hxf.domain.Palmary;
import com.ruoyi.hxf.mapper.PalmaryMapper;
import com.ruoyi.hxf.service.IPalmaryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 学校介绍管理Service业务层处理
 *
 * @author hxf
 * @date 2023-04-08
 */
@RequiredArgsConstructor
@Service
public class PalmaryServiceImpl implements IPalmaryService {

    private final PalmaryMapper baseMapper;

    /**
     * 查询学校介绍管理
     */
    @Override
    public PalmaryVo queryById(Long palmaryId){
        return baseMapper.selectVoById(palmaryId);
    }

    /**
     * 查询学校介绍管理列表
     */
    @Override
    public TableDataInfo<PalmaryVo> queryPageList(PalmaryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Palmary> lqw = buildQueryWrapper(bo).last("order by order_by asc");
        Page<PalmaryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
    /**
     * 查询学校介绍管理列表
     */
    @Override
    public TableDataInfo<PalmaryVo> queryPageAppList(PalmaryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Palmary> lqw = buildQueryWrapper(bo).eq(Palmary::getStatus,0).last("order by order_by asc");
        Page<PalmaryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
    /**
     * 查询学校介绍管理列表
     */
    @Override
    public List<PalmaryVo> queryList(PalmaryBo bo) {
        LambdaQueryWrapper<Palmary> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Palmary> buildQueryWrapper(PalmaryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Palmary> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增学校介绍管理
     */
    @Override
    public Boolean insertByBo(PalmaryBo bo) {
        Palmary add = BeanUtil.toBean(bo, Palmary.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPalmaryId(add.getPalmaryId());
        }
        return flag;
    }

    /**
     * 修改学校介绍管理
     */
    @Override
    public Boolean updateByBo(PalmaryBo bo) {
        Palmary update = BeanUtil.toBean(bo, Palmary.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Palmary entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除学校介绍管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
